<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Canvas | latte and cat</title>
    <meta name="description" content="study programs and record life">
    <meta name="generator" content="VuePress 1.4.0">
    <link rel="icon" href="/favicon.ico">
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.e1c67194.css" as="style"><link rel="preload" href="/assets/js/app.ff77de4f.js" as="script"><link rel="preload" href="/assets/js/4.6c1b7be2.js" as="script"><link rel="preload" href="/assets/js/1.3ea07518.js" as="script"><link rel="preload" href="/assets/js/18.1d703305.js" as="script"><link rel="prefetch" href="/assets/js/10.18024e3f.js"><link rel="prefetch" href="/assets/js/11.a9ba7471.js"><link rel="prefetch" href="/assets/js/12.64cdb496.js"><link rel="prefetch" href="/assets/js/13.9fb4b156.js"><link rel="prefetch" href="/assets/js/14.c3753656.js"><link rel="prefetch" href="/assets/js/15.652b11fc.js"><link rel="prefetch" href="/assets/js/16.b24685ca.js"><link rel="prefetch" href="/assets/js/17.67155a52.js"><link rel="prefetch" href="/assets/js/19.0b6a6748.js"><link rel="prefetch" href="/assets/js/20.a576349c.js"><link rel="prefetch" href="/assets/js/21.4bd94152.js"><link rel="prefetch" href="/assets/js/22.1abe4122.js"><link rel="prefetch" href="/assets/js/23.d2352828.js"><link rel="prefetch" href="/assets/js/24.a69cf77c.js"><link rel="prefetch" href="/assets/js/25.40b2c71a.js"><link rel="prefetch" href="/assets/js/26.7da4b904.js"><link rel="prefetch" href="/assets/js/27.80a148eb.js"><link rel="prefetch" href="/assets/js/28.d8cf3072.js"><link rel="prefetch" href="/assets/js/5.60884555.js"><link rel="prefetch" href="/assets/js/6.e3bfbe92.js"><link rel="prefetch" href="/assets/js/7.f4dfd5c4.js"><link rel="prefetch" href="/assets/js/8.14ec5fab.js"><link rel="prefetch" href="/assets/js/9.e4d60889.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.af2c2885.js">
    <link rel="stylesheet" href="/assets/css/0.styles.e1c67194.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div><div class="theme-container" data-v-319dd33c><div data-v-319dd33c><div id="loader-wrapper" class="loading-wrapper" data-v-4b73742e data-v-319dd33c data-v-319dd33c><div class="loader-main" data-v-4b73742e><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-1e2a0cc0 data-v-319dd33c data-v-319dd33c><h3 class="title" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0>latte and cat</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><input type="password" value="" data-v-1e2a0cc0> <span data-v-1e2a0cc0>Konck! Knock!</span> <button data-v-1e2a0cc0>OK</button></label> <div class="footer" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><span data-v-1e2a0cc0><i class="iconfont reco-theme" data-v-1e2a0cc0></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-1e2a0cc0>vuePress-theme-reco</a></span> <span data-v-1e2a0cc0><i class="iconfont reco-copyright" data-v-1e2a0cc0></i> <a data-v-1e2a0cc0><span data-v-1e2a0cc0>橘子</span>
            
          <span data-v-1e2a0cc0>2019 - </span>
          2020
        </a></span></div></div> <div class="hide" data-v-319dd33c><header class="navbar" data-v-319dd33c><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/head.png" alt="latte and cat" class="logo"> <span class="site-name">latte and cat</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/笔记/" class="nav-link"><i class="iconfont undefined"></i>
  笔记
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/摘记/" class="nav-link"><i class="iconfont undefined"></i>
  摘记
</a></li><li class="dropdown-item"><!----> <a href="/categories/随笔/" class="nav-link"><i class="iconfont undefined"></i>
  随笔
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/note/" class="nav-link router-link-active"><i class="iconfont reco-document"></i>
  Notes
</a></div><div class="nav-item"><a href="/timeLine/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><a href="http://photo.smallsunnyfox.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-blog"></i>
  PhotoAlbum
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/smallsunnyfox" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-319dd33c></div> <aside class="sidebar" data-v-319dd33c><div class="personal-info-wrapper" data-v-6c8ffc9c><!----> <h3 class="name" data-v-6c8ffc9c>
    橘子
  </h3> <div class="num" data-v-6c8ffc9c><div data-v-6c8ffc9c><h3 data-v-6c8ffc9c>14</h3> <h6 data-v-6c8ffc9c>文章</h6></div> <div data-v-6c8ffc9c><h3 data-v-6c8ffc9c>6</h3> <h6 data-v-6c8ffc9c>标签</h6></div></div> <hr data-v-6c8ffc9c></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/笔记/" class="nav-link"><i class="iconfont undefined"></i>
  笔记
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/摘记/" class="nav-link"><i class="iconfont undefined"></i>
  摘记
</a></li><li class="dropdown-item"><!----> <a href="/categories/随笔/" class="nav-link"><i class="iconfont undefined"></i>
  随笔
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/note/" class="nav-link router-link-active"><i class="iconfont reco-document"></i>
  Notes
</a></div><div class="nav-item"><a href="/timeLine/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><a href="http://photo.smallsunnyfox.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-blog"></i>
  PhotoAlbum
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/smallsunnyfox" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>HTML5</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/note/html5/HTML5的语义元素.html" class="sidebar-link">HTML5语义元素</a></li><li><a href="/note/html5/HTML5多媒体标签.html" class="sidebar-link">HTML5多媒体标签</a></li><li><a href="/note/html5/HTML5表单元素.html" class="sidebar-link">HTML5表单元素</a></li><li><a href="/note/html5/HTML5中的API.html" class="sidebar-link">HTML5中的API</a></li><li><a href="/note/html5/Canvas.html" class="active sidebar-link">Canvas</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/note/html5/Canvas.html#绘图工具" class="sidebar-link">绘图工具</a></li><li class="sidebar-sub-header"><a href="/note/html5/Canvas.html#绘图方法" class="sidebar-link">绘图方法</a></li><li class="sidebar-sub-header"><a href="/note/html5/Canvas.html#渐变方案" class="sidebar-link">渐变方案</a></li><li class="sidebar-sub-header"><a href="/note/html5/Canvas.html#填充效果" class="sidebar-link">填充效果</a></li><li class="sidebar-sub-header"><a href="/note/html5/Canvas.html#非零环绕原则" class="sidebar-link">非零环绕原则</a></li><li class="sidebar-sub-header"><a href="/note/html5/Canvas.html#绘制虚线" class="sidebar-link">绘制虚线</a></li><li class="sidebar-sub-header"><a href="/note/html5/Canvas.html#绘制矩形" class="sidebar-link">绘制矩形</a></li><li class="sidebar-sub-header"><a href="/note/html5/Canvas.html#绘制文本" class="sidebar-link">绘制文本</a></li><li class="sidebar-sub-header"><a href="/note/html5/Canvas.html#绘制图片" class="sidebar-link">绘制图片</a></li><li class="sidebar-sub-header"><a href="/note/html5/Canvas.html#绘制圆弧" class="sidebar-link">绘制圆弧</a></li><li class="sidebar-sub-header"><a href="/note/html5/Canvas.html#坐标系变换" class="sidebar-link">坐标系变换</a></li></ul></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>CSS</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/note/css/css3新特性.html" class="sidebar-link">CSS3新特性</a></li><li><a href="/note/css/css3Flip.html" class="sidebar-link">CSS3实现翻转(Flip)效果</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>前端单元测试</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/note/fe-unit-test/chai.html" class="sidebar-link">chai.js笔记</a></li><li><a href="/note/fe-unit-test/mocha.html" class="sidebar-link">Mocha笔记</a></li><li><a href="/note/fe-unit-test/vueTestUtils.html" class="sidebar-link">VueTestUtils笔记</a></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-1e2a0cc0 data-v-319dd33c><h3 class="title" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0>Canvas</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><input type="password" value="" data-v-1e2a0cc0> <span data-v-1e2a0cc0>Konck! Knock!</span> <button data-v-1e2a0cc0>OK</button></label> <div class="footer" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><span data-v-1e2a0cc0><i class="iconfont reco-theme" data-v-1e2a0cc0></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-1e2a0cc0>vuePress-theme-reco</a></span> <span data-v-1e2a0cc0><i class="iconfont reco-copyright" data-v-1e2a0cc0></i> <a data-v-1e2a0cc0><span data-v-1e2a0cc0>橘子</span>
            
          <span data-v-1e2a0cc0>2019 - </span>
          2020
        </a></span></div></div> <div data-v-319dd33c><main class="page"><div class="page-title" style="display:none;"><h1>Canvas</h1> <hr> <div data-v-484a899e><i class="iconfont reco-account" data-v-484a899e><span data-v-484a899e>橘子</span></i> <!----> <i class="iconfont reco-eye" data-v-484a899e><span id="/note/html5/Canvas.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-484a899e><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-484a899e><span class="tag-item" data-v-484a899e>
      Canvas
    </span><span class="tag-item" data-v-484a899e>
      Html5
    </span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h2 id="绘图工具"><a href="#绘图工具" class="header-anchor">#</a> 绘图工具</h2> <p>canvas画布</p> <ul><li>默认大小为300*200</li> <li>可通过width属性和height属性设置画布大小</li> <li>解决画布重绘问题：描边后使用 <code>beginPath()</code> 开启新的图层</li></ul> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>canvas</span> <span class="token attr-name">width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>600<span class="token punctuation">&quot;</span></span> <span class="token attr-name">height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>400<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>canvas</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="绘图方法"><a href="#绘图方法" class="header-anchor">#</a> 绘图方法</h2> <p>线连接方式：   lineJoin: round | bevel | miter (默认)</p> <p>线帽（线两端的结束方式）：  lineCap: butt(默认值) | round | square</p> <p>闭合路径： ctx.closePath()</p> <p>开启新的图层： ctx.beginPath()</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 获取canvas画布对象</span>
<span class="token keyword">var</span> canvas <span class="token operator">=</span>document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">&quot;canvas&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 获取绘图上下文</span>
<span class="token keyword">var</span> ctx<span class="token operator">=</span>canvas<span class="token punctuation">.</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token string">&quot;2d&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 落笔</span>
ctx<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span>
<span class="token comment">// 连线</span>
ctx<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span>
<span class="token comment">// 设置线的颜色为红色</span>
ctx<span class="token punctuation">.</span>strokeStyle<span class="token operator">=</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">;</span>
<span class="token comment">// 设置线宽</span>
ctx<span class="token punctuation">.</span>lineWidth<span class="token operator">=</span><span class="token string">&quot;20&quot;</span><span class="token punctuation">;</span>
<span class="token comment">// 描边</span>
ctx<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h2 id="渐变方案"><a href="#渐变方案" class="header-anchor">#</a> 渐变方案</h2> <h3 id="线性渐变"><a href="#线性渐变" class="header-anchor">#</a> 线性渐变</h3> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// x0--&gt;渐变开始的x坐标</span>
<span class="token comment">// y0--&gt;渐变开始的y坐标</span>
<span class="token comment">// x1--&gt;渐变结束的x坐标</span>
<span class="token comment">// y1--&gt;渐变结束的y坐标</span>
<span class="token keyword">var</span> grd<span class="token operator">=</span>ctx<span class="token punctuation">.</span><span class="token function">createLinearGradient</span><span class="token punctuation">(</span>x0<span class="token punctuation">,</span>y0<span class="token punctuation">,</span>x1<span class="token punctuation">,</span>y1<span class="token punctuation">)</span><span class="token punctuation">;</span>  
      
<span class="token comment">// 设置渐变的开始颜色</span>
grd<span class="token punctuation">.</span><span class="token function">addColorStop</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token string">&quot;black&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      
<span class="token comment">// 设置渐变的中间颜色</span>
grd<span class="token punctuation">.</span><span class="token function">addColorStop</span><span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">,</span><span class="token string">&quot;yellow&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   
<span class="token comment">// 设置渐变的结束颜色</span>
grd<span class="token punctuation">.</span><span class="token function">addColorStop</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>        

ctx<span class="token punctuation">.</span>strokeStyle<span class="token operator">=</span>grd<span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>备注：addColorStop(offse,color)中渐变的开始位置和结束位置介于0-1之间，0代表开始，1代表结束。中间可以设置任何小数。</p> <h3 id="径向渐变"><a href="#径向渐变" class="header-anchor">#</a> 径向渐变</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>ctx.createradialGradient(x0,y0,r0,x1,y1,r1);

(x0,y0)：渐变的开始圆的 x,y 坐标

r0：开始圆的半径

(x1,y1)：渐变的结束圆的 x,y 坐标

r1：结束圆的半径
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h2 id="填充效果"><a href="#填充效果" class="header-anchor">#</a> 填充效果</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code>ctx.fillstyle=&quot;值&quot;;    设置填充颜色
ctx.fill();	          设置填充效果
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="非零环绕原则"><a href="#非零环绕原则" class="header-anchor">#</a> 非零环绕原则</h2> <ol><li>任意找一点，越简单越好</li> <li>以点为圆心，绘制一条射线，越简单越好（相交的边越少越好）</li> <li>以射线为半径顺时针旋转，相交的边同向记为+1，反方向记为-1，如果相加的区域等于0，则不填充。（同向和反向指的是旋转方向和绘制方向是否相同）</li> <li>非零区域填充<br>
例子：回字图案</li></ol> <h2 id="绘制虚线"><a href="#绘制虚线" class="header-anchor">#</a> 绘制虚线</h2> <ul><li>原理：设置虚线其实就是设置实线与空白部分直接的距离,利用数组描述其中的关系</li> <li>绘制
<ul><li>ctx.setLineDash(数组);</li> <li>ctx.stroke();</li></ul></li> <li>实线和空白不断根据数组的值循环</li> <li>如果要将虚线改为实线，只要将数组改为空数组即可</li></ul> <h2 id="绘制矩形"><a href="#绘制矩形" class="header-anchor">#</a> 绘制矩形</h2> <ul><li>绘制一个描边矩形： <code>content.strokeRect(x,y,width,height)</code></li> <li>绘制一个填充矩形： <code>content.fillRect(x,y,width,height)</code></li> <li>清除：		   <code>content.clearRect(x,y,width,height)</code></li> <li>实现动画矩形
<ul><li>先清屏</li> <li>绘制图形</li> <li>处理变量</li></ul></li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//开始位置</span>
<span class="token keyword">var</span> x<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token comment">//处理的是一个增量</span>
<span class="token keyword">var</span> step<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">;</span>
<span class="token comment">//改变方向变量</span>
<span class="token keyword">var</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
<span class="token comment">//创建一个定时器</span>
<span class="token function">setInterval</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token comment">//先清屏</span>
	ctx<span class="token punctuation">.</span><span class="token function">clearRect</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> canvas<span class="token punctuation">.</span>width<span class="token punctuation">,</span> canvas<span class="token punctuation">.</span>height<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token comment">//开始绘制图形</span>
	ctx<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token comment">//开始处理变量</span>
	x<span class="token operator">+=</span>step<span class="token operator">*</span>i<span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>x<span class="token operator">&gt;</span>canvas<span class="token punctuation">.</span>width<span class="token operator">-</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
		i<span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>x<span class="token operator">&lt;</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
		i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h2 id="绘制文本"><a href="#绘制文本" class="header-anchor">#</a> 绘制文本</h2> <ul><li><p>绘制填充文本：<code>content.fillText(文本的内容,x,y)</code></p></li> <li><p>绘制镂空文本：<code>content.strokeText()</code></p></li> <li><p>设置文字大小：</p> <ul><li><code>content.font=&quot;20px 微软雅黑&quot;</code></li> <li>备注： 该属性设置文字大小，必须按照cssfont属性的方式设置</li></ul></li> <li><p>文字水平对齐方式</p> <ul><li><code>content.textalign=&quot;left | right | center&quot;</code></li> <li>文字在圆心点位置的对齐方式</li></ul></li> <li><p>文字垂直对齐方式</p> <ul><li><code>content.textBaseline=&quot;top | middle | bottom | alphabetic(默认)&quot;</code></li></ul></li> <li><p>文字阴影效果</p> <ul><li>设置文字阴影的颜色 <code>ctx.shadowColor=&quot;red&quot;</code></li> <li>设置文字阴影的水平偏移量 <code>ctx.ShadowOffsetX=值</code></li> <li>设置文字阴影的垂直偏移量 <code>ctx.shadowOffsetY=值</code></li> <li>设置文字阴影的模糊度 <code>ctx.shadowBlur=值</code></li></ul></li></ul> <h2 id="绘制图片"><a href="#绘制图片" class="header-anchor">#</a> 绘制图片</h2> <ul><li>将图片绘制到画布的指定位置
<ul><li><code>content.drawImage(图片对象,x,y)</code></li></ul></li> <li>将图片绘制到指定区域大小的位置
<ul><li><code>content.drawImage(图片对象,x,y,width,height);</code></li> <li>x,y指的是矩形区域的位置</li> <li>width和height指的是矩形区域的大小</li></ul></li> <li>将图片的指定区域绘制到指定矩形区域内
<ul><li><code>content.drawImage(图片对象,sx,sy,swidth,sheight,dx,dy,dwidth,dheight);</code></li> <li>sx,sy 指的是要从图片哪块区域开始绘制</li> <li>swidth，sheight 是指截取图片区域的大小</li> <li>dx,dy 是指矩形区域的位置</li> <li>dwidth,dheight是指矩形区域的大小</li></ul></li> <li>解决图片绘制到某一个区域的按比例缩放绘制
<ul><li>绘制宽：绘制高 == 原始宽：原始高</li></ul></li></ul> <h2 id="绘制圆弧"><a href="#绘制圆弧" class="header-anchor">#</a> 绘制圆弧</h2> <ul><li><code>content.arc(x,y,radius,startradian,endradian[,direct]);</code> <ul><li>(x,y) 为圆心的坐标</li> <li>radius 为半径</li> <li>startradian 为开始弧度</li> <li>endradian 为结束弧度</li> <li>direct 为方向（默认顺时针 false）true 代表逆时针</li></ul></li> <li>以圆心为中心向右为0角 顺时针为正，逆时针为负</li> <li>角度 和 弧度的关系： 角度:弧度= 180:pi</li></ul> <h2 id="坐标系变换"><a href="#坐标系变换" class="header-anchor">#</a> 坐标系变换</h2> <h3 id="平移"><a href="#平移" class="header-anchor">#</a> 平移</h3> <p>坐标系原点的平移：<code>ctx.translate(x,y);</code></p> <p>通过该方法可以将原点的位置进行重新设置。</p> <p>注意</p> <ul><li>translate(x,y) 中不能设置一个值</li> <li>与moveTo(x,y) 的区别
<ul><li>moveTo(x,y) 指的是将画笔的落笔点的位置改变，而坐标系中的原点位置并没有发生改变</li> <li>translate(x,y) 是将坐标系中的原点位置发生改变</li></ul></li></ul> <h3 id="旋转"><a href="#旋转" class="header-anchor">#</a> 旋转</h3> <p>坐标系旋转：<code>ctx.rotate(弧度)</code></p> <h3 id="伸缩"><a href="#伸缩" class="header-anchor">#</a> 伸缩</h3> <p>沿着x轴和y轴缩放：<code>ctx.scale(x,y)</code></p> <p>x,y 为倍数  例如： 0.5  1</p></div> <footer class="page-edit" style="display:none;"><!----> <!----></footer> <!----></main> <!----> <div style="display:none;" data-v-319dd33c data-v-319dd33c><div class="comments-wrapper" data-v-319dd33c><div class="valine-wrapper"><div id="valine"></div></div></div></div></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-a81d141e data-v-a81d141e><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-a81d141e><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-a81d141e></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-a81d141e></path></svg></div></div></div>
    <script src="/assets/js/app.ff77de4f.js" defer></script><script src="/assets/js/4.6c1b7be2.js" defer></script><script src="/assets/js/1.3ea07518.js" defer></script><script src="/assets/js/18.1d703305.js" defer></script>
  </body>
</html>
